package com.dimong.loan.sdk.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.ViewGroup;

/**
 * Created by jiangzhenwei on 2017/7/27.
 */

public abstract class StaggeredGridLayoutAdapter<T> extends BaseRecyclerAdapter<T> {

    private StaggeredGridLayoutSpanInterface mStaggeredGridLayoutSpanInterface;

    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if (isStaggeredGridLayout(holder)) {
            handleLayoutIfStaggeredGridLayout(holder, holder.getLayoutPosition());
        }
    }

    private boolean isStaggeredGridLayout(RecyclerView.ViewHolder holder) {
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        if (layoutParams != null && layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
            return true;
        }
        return false;
    }

    protected void handleLayoutIfStaggeredGridLayout(RecyclerView.ViewHolder holder, int position) {
        StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        if(null != mStaggeredGridLayoutSpanInterface) {
            if (mStaggeredGridLayoutSpanInterface.isHeaderSpan(position)
                    || mStaggeredGridLayoutSpanInterface.isFooterSpan(position)
                    || mStaggeredGridLayoutSpanInterface.isHorDataSpan(position)) {
                p.setFullSpan(true);
            }else{
                p.setFullSpan(false);
            }
        }else {
            p.setFullSpan(false);
        }
    }

    public interface StaggeredGridLayoutSpanInterface{
        boolean isHeaderSpan(int position);
        boolean isFooterSpan(int position);
        boolean isHorDataSpan(int position);
    }

    public void setStaggeredGridLayoutSpanInterface(StaggeredGridLayoutSpanInterface mStaggeredGridLayoutSpanInterface) {
        this.mStaggeredGridLayoutSpanInterface = mStaggeredGridLayoutSpanInterface;
    }
}
